﻿using Microsoft.Office.Tools;
//using Tools = Microsoft.Office.Tools;
using Word = Microsoft.Office.Interop.Word;


namespace docTissV2
{
    public partial class ThisAddIn
    {
        public object _missing;

        private object indexNumeroGuiaCC = 2;
        public object IndexNumeroGuiaCC
        {
            get { return indexNumeroGuiaCC; }
            set { indexNumeroGuiaCC = value; }
        }

        public CustomTaskPane GerenciaGuiaCTP { get; set; }
        public CustomTaskPane ModuloConfiguracaoCTP { get; set; }
        public CustomTaskPane CriarLoginCTP { get; set; }
        public CustomTaskPane LoginCTP { get; set; }
        public CustomTaskPane CodigoPrestadorCTP { get; set; }
        public CustomTaskPane DenteRegiaoCTP { get; set; }
        public CustomTaskPane ObitoNeonatalCTP { get; set; }
        public CustomTaskPane NumeroGuiaCTP { get; set; }

        public Word.Document doc = null;

        public bool guiaLoaded = false;      

       private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                _missing = this.missing;
                if (this.Application.Documents.Count > 0)
                    this.doc = this.Application.ActiveDocument;
                this.Application.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
            }
            catch { }
        }

        void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
        {
            foreach (Microsoft.Office.Core.CustomXMLPart ccPart in Doc.CustomXMLParts)
            {
                if (ccPart.XML.Contains("http://www.ans.gov.br/padroes/tiss/schemas"))
                {
                    this.doc = Doc;

                    doc.ContentControlOnEnter += new Microsoft.Office.Interop.Word.DocumentEvents2_ContentControlOnEnterEventHandler(doc_ContentControlOnEnter);
                    doc.ContentControlOnExit += new Microsoft.Office.Interop.Word.DocumentEvents2_ContentControlOnExitEventHandler(doc_ContentControlOnExit);

                    foreach (Microsoft.Office.Interop.Word.ContentControl cc in Doc.ContentControls)
                    {
                        if (cc.Range.Text == "Editar")
                            cc.SetPlaceholderText(null, null, "Editar");
                    }
                }
            }
        }

        void doc_ContentControlOnExit(Microsoft.Office.Interop.Word.ContentControl ContentControl, ref bool Cancel)
        {
            if ((ContentControl.PlaceholderText.Value == "") || (ContentControl.Range.Text == null) ||
                (ContentControl.PlaceholderText.Value == null) || (ContentControl.Range.Text == ""))
            {
                ContentControl.SetPlaceholderText(null, null, "Editar");
            }
            if (ContentControl.Tag != null)
            {
                if (ContentControl.Tag.Contains("codigoPrestador"))
                {
                    if (Globals.ThisAddIn.CodigoPrestadorCTP != null && Globals.ThisAddIn.CodigoPrestadorCTP.Visible == true)
                    {
                        Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.CodigoPrestadorCTP);
                    }
                }
            }

        }

        void doc_ContentControlOnEnter(Microsoft.Office.Interop.Word.ContentControl ContentControl)
        {
            //if ((ContentControl.PlaceholderText.Value == "Editar") && (ContentControl.Range.Text == "Editar"))
            //{
            //    ContentControl.SetPlaceholderText(null, null, "");
            //    ContentControl.Range.Text = "";
            //}
            string xPath = null;
            uc_CodigoPrestador codPrestador;
            uc_DenteRegiao denteRegiao;
            uc_ObitoNeonatal obitoNeonatal;

            switch (ContentControl.Tag)
            {
                case "codigoPrestador":
                    xPath = ContentControl.XMLMapping.XPath.Replace("ns0", "ans");
                    codPrestador = new uc_CodigoPrestador("", xPath, ContentControl);
                    codPrestador.identificacaoChanged += new uc_CodigoPrestador.identificacaoSelectedHandler(customControlChoiceItemChanged);
                    CodigoPrestadorCTP = Globals.ThisAddIn.CustomTaskPanes.Add(codPrestador, "Identificação");
                    CodigoPrestadorCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    CodigoPrestadorCTP.Width = 150;
                    CodigoPrestadorCTP.Height = 185;
                    CodigoPrestadorCTP.Visible = true;
                    break;
                case "codigoPrestadorSemCNPJ":
                    xPath = ContentControl.XMLMapping.XPath.Replace("ns0", "ans");
                    codPrestador = new uc_CodigoPrestador("CNPJ", xPath, ContentControl);
                    codPrestador.identificacaoChanged += new uc_CodigoPrestador.identificacaoSelectedHandler(customControlChoiceItemChanged);
                    CodigoPrestadorCTP = Globals.ThisAddIn.CustomTaskPanes.Add(codPrestador, "Identificação");
                    CodigoPrestadorCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    CodigoPrestadorCTP.Width = 150;
                    CodigoPrestadorCTP.Height = 185;
                    CodigoPrestadorCTP.Visible = true;
                    break;
                case "codigoPrestadorSemCPF":
                    xPath = ContentControl.XMLMapping.XPath.Replace("ns0", "ans");
                    codPrestador = new uc_CodigoPrestador("CPF", xPath, ContentControl);
                    codPrestador.identificacaoChanged += new uc_CodigoPrestador.identificacaoSelectedHandler(customControlChoiceItemChanged);
                    CodigoPrestadorCTP = Globals.ThisAddIn.CustomTaskPanes.Add(codPrestador, "Identificação");
                    CodigoPrestadorCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    CodigoPrestadorCTP.Width = 150;
                    CodigoPrestadorCTP.Height = 185;
                    CodigoPrestadorCTP.Visible = true;
                    break;
                case "denteRegiao":
                    xPath = ContentControl.XMLMapping.XPath.Replace("ns0", "ans");
                    denteRegiao = new uc_DenteRegiao(xPath, ContentControl);
                    denteRegiao.denteRegiaoChanged += new uc_DenteRegiao.denteRegiaoSelectedHandler(customControlChoiceItemChanged);
                    DenteRegiaoCTP = Globals.ThisAddIn.CustomTaskPanes.Add(denteRegiao, "Dente/Região");
                    DenteRegiaoCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    DenteRegiaoCTP.Width = 165;
                    DenteRegiaoCTP.Height = 185;
                    DenteRegiaoCTP.Visible = true;
                    break;
                case "obitoNeonatal":
                    xPath = ContentControl.XMLMapping.XPath.Replace("ns0", "ans");
                    obitoNeonatal = new uc_ObitoNeonatal(xPath, ContentControl);
                    obitoNeonatal.obitoNeonatalChanged += new uc_ObitoNeonatal.obitoNeonatalSelectedHandler(customControlChoiceItemChanged);
                    ObitoNeonatalCTP = Globals.ThisAddIn.CustomTaskPanes.Add(obitoNeonatal, "Óbito Neonatal");
                    ObitoNeonatalCTP.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    ObitoNeonatalCTP.Width = 165;
                    ObitoNeonatalCTP.Height = 185;
                    ObitoNeonatalCTP.Visible = true;
                    break;
                default:
                    break;
            }
        }

        void customControlChoiceItemChanged(string identificacao, string xPath, Microsoft.Office.Interop.Word.ContentControl cc)
        {
            foreach (Microsoft.Office.Core.CustomXMLPart ccPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)
            {
                if (ccPart.XML.Contains("ans"))
                {
                    ccPart.NamespaceManager.AddNamespace("ans", "http://www.ans.gov.br/padroes/tiss/schemas");
                    Microsoft.Office.Core.CustomXMLNode ccNode = null;

                    if (identificacao.Equals("cpf") || identificacao.Equals("CPF"))
                    {
                        if (xPath.Contains("/ans:dadosSolicitante[1]/ans:contratado[1]/ans:identificacao[1]") ||
                            xPath.Contains("/ans:prestadorExecutante[1]/ans:profissionalExecutanteCompl[1]/ans:codigoProfissionalCompl[1]") ||
                            xPath.Contains("/ans:procedimentos[1]/ans:equipe[1]/ans:membroEquipe[1]"))
                        {
                            identificacao = identificacao.ToLower();
                        }
                        else
                        {
                            identificacao = identificacao.ToUpper();
                        }
                    }

                    ccNode = ccPart.DocumentElement.SelectSingleNode(xPath);
                    if (ccNode != null)
                    {
                        ccNode.ParentNode.ReplaceChildNode(ccNode, "ans:" + identificacao, "http://www.ans.gov.br/padroes/tiss/schemas", Microsoft.Office.Core.MsoCustomXMLNodeType.msoCustomXMLNodeElement, "");
                        string newXPath = xPath.Replace(xPath.Substring(xPath.LastIndexOf('/') + 1), "ns0:" + identificacao).Replace("ans", "ns0") + "[1]";
                        cc.XMLMapping.SetMapping(newXPath, null, null);
                    }
                    break;
                }
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
